namespace Gamov.DeleteSdData.Main.Services.Impl
{
    using System.IO;
    using Gamov.DeleteSdData.Main.Frame.Attributes;
    using Gamov.DeleteSdData.Main.Services;
    using Microsoft.Extensions.Logging;

    [Component(typeof(IFileService), Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton)]
    public class FileService : IFileService
    {

        private readonly ILogger<FileService> _logger;

        public FileService(ILogger<FileService> logger)
        {
            _logger = logger;
        }

        public void ClearDirectory(string path)
        {
            _logger.LogInformation($"Start to clear the directory: [{path}]");
            try
            {
                if (Directory.Exists(path))
                {
                    //获取该路径下的文件路径
                    var filePathList = Directory.GetFiles(path);
                    foreach (var filePath in filePathList)
                    {
                        File.Delete(filePath);
                    }

                    //获取该路径下的文件夹路径
                    var directorsList = Directory.GetDirectories(path);
                    foreach (var directory in directorsList)
                    {
                        Directory.Delete(directory, true);//删除该文件夹及该文件夹下包含的文件
                    }
                }
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, $"An error occurred when clear directory:{path}.");
            }
        }

        /// <summary>
        /// 复制源文件夹下的所有内容到新文件夹
        /// </summary>
        /// <param name="sources">源文件夹路径</param>
        /// <param name="dest">新文件夹路径</param>
        public void CopyFolder(string sources, string dest)
        {
            try
            {
                if (!Directory.Exists(dest))
                {
                    _logger.LogInformation($"Directory:[{dest}] is not exist.");
                    return;
                }

                var dinfo = new DirectoryInfo(sources);
                //注，这里面传的是路径，并不是文件，所以不能包含带后缀的文件                
                foreach (var f in dinfo.GetFileSystemInfos())
                {
                    //目标路径destName = 新文件夹路径 + 源文件夹下的子文件(或文件夹)名字                
                    //Path.Combine(string a ,string b) 为合并两个字符串                     
                    var destName = Path.Combine(dest, f.Name);
                    if (f is FileInfo)
                    {
                        //如果是文件就复制       
                        File.Copy(f.FullName, destName, true);//true代表可以覆盖同名文件                     
                    }
                    else
                    {
                        //如果是文件夹就创建文件夹，然后递归复制              
                        Directory.CreateDirectory(destName);
                        CopyFolder(f.FullName, destName);
                    }
                }
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, $"An error occurred when copy folder from [{sources}] to [{dest}].");
            }
        }
    }
}
